Skip to content

Add fn load_workspace_into_db variant for ra_ap_load_cargo's fn load_workspace #20144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

regexident
Copy link
Contributor

See code comment for motivation:

pub fn load_workspace(
    ws: ProjectWorkspace,
    extra_env: &FxHashMap<String, Option<String>>,
    load_config: &LoadCargoConfig,
) -> anyhow::Result<(RootDatabase, vfs::Vfs, Option<ProcMacroClient>)> {
    let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<u16>().ok());
    let mut db = RootDatabase::new(lru_cap);

    let (vfs, proc_macro_server) = load_workspace_into_db(ws, extra_env, load_config, &mut db)?;

    Ok((db, vfs, proc_macro_server))
}

// This variant of `load_workspace` allows deferring the loading of rust-analyzer
// into an existing database, which is useful in certain third-party scenarios,
// now that `salsa` supports extending foreign databases (e.g. `RootDatabase`).
pub fn load_workspace_into_db(
    ws: ProjectWorkspace,
    extra_env: &FxHashMap<String, Option<String>>,
    load_config: &LoadCargoConfig,
    db: &mut RootDatabase,
) -> anyhow::Result<(vfs::Vfs, Option<ProcMacroClient>)> {
    // ...
}

Specific use case motivating this change: ra_ap_project_model's flavor of metadata is not as rich as cargo_metadata's Metadata, so a third-party may want to extend RootDatabase with the metadata from cargo_metadata, but without having to first load the entire project (i.e. its entire source code and all). Instead it would be preferable to be able to defer loading via ra_ap_load_cargo until after cargo_metadata has been loaded (and most importantly: already been made use of).

Imagine a tool that first performs package/target/crate discovery via cargo_metadata, then shows an overview of the project at hand, allowing for selecting individual items of interest and only upon confirmation does the actual heavy lifting of loading the project workspace via ra_ap_load_cargo. The current API would require one to create a temporary database for loading the cargo_metadata stuff to do the discovery and then discard/replace that database with RootDatabase, which then requires the cargo_metadata loading to be performed a second time on that new db.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 2, 2025
@regexident regexident force-pushed the load-workspace-into-db branch from 4c0dbd1 to 8ea81c6 Compare July 2, 2025 09:20
@regexident regexident force-pushed the load-workspace-into-db branch from 8ea81c6 to a0e857f Compare July 3, 2025 21:09
Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me.

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Jul 3, 2025
Merged via the queue into rust-lang:master with commit 040049b Jul 3, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 3, 2025
@regexident regexident deleted the load-workspace-into-db branch July 4, 2025 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants